/*
 * File:   Obstacle.cpp
 * Author: Elevator
 *
 * Created on August 2, 2013, 11:44 PM
 */
#include "Physics/Shapes/IShape.h"
#include "Physics/Colliders/ICollider.h"
#include "Physics/Bodies/Body.h"
#include "Logic/PhysicsDebugDrawer.h"
#include "Obstacle.h"

Obstacle::Obstacle( std::string id ) :
	_id( id )
{
}

Obstacle::Obstacle( std::string id, IIterable<physics::IBody*>* bodies ) :
	_id( id )
{
	for( IteratorPtr<physics::IBody*> obstacles( bodies->GetIterator( ) );
			!obstacles->IsEnd( );
			obstacles->Next( ) )
	{
		_bodies.Add( obstacles->Current( ) );
	}
}

Obstacle::~Obstacle( )
{
}

void Obstacle::AddBody( physics::IBody* body )
{
	_bodies.Add( body );
}

void Obstacle::RemoveBody( physics::IBody* body )
{
	_bodies.Remove( body );
}

void Obstacle::Update( float )
{
}

void Obstacle::Draw( ) const
{
	PhysicsDebugDrawer* drawer = new PhysicsDebugDrawer(
		core::Color(0.2f, 1.0f, 0.2f),
		core::Color(0.2f, 1.0f, 0.2f, 0.2f));

	for(
		IteratorPtr<physics::IBody*> bodyIter(_bodies.GetIterator());
		!bodyIter->IsEnd();
		bodyIter->Next() )
	{
		physics::IBody* body = bodyIter->Current();

		drawer->SetPosition(body->GetPos());

		for(
			IteratorPtr<const physics::ICollider*> colliderIter(body->GetColliders()->GetIterator());
			!colliderIter->IsEnd();
			colliderIter->Next() )
		{
			const physics::ICollider* collider = colliderIter->Current();
			const physics::IShape* shape = collider->GetShape();

			shape->Accept(drawer);
		}
	}
	delete drawer;
}
